package cn.yangzhi.demo;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

public class MainActivity extends AppCompatActivity {

    private MaterialButton btnLike;
    private MaterialButton btnFavorite;
    private boolean isLiked = false;
    private boolean isFavorited = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        // 初始化视图
        initViews();
        // 设置点击事件
        setupClickListeners();
        // 设置窗口边距
        setupWindowInsets();
    }

    private void initViews() {
        // 初始化按钮
        btnLike = findViewById(R.id.btnLike);
        btnFavorite = findViewById(R.id.btnFavorite);
    }

    private void setupClickListeners() {
        // 点赞按钮点击事件
        btnLike.setOnClickListener(v -> {
            isLiked = !isLiked;
            updateLikeButton();
            showToast(isLiked ? "点赞成功" : "取消点赞");
        });

        // 收藏按钮点击事件
        btnFavorite.setOnClickListener(v -> {
            isFavorited = !isFavorited;
            updateFavoriteButton();
            showToast(isFavorited ? "收藏成功" : "取消收藏");
        });

        // 作者头像点击事件
        findViewById(R.id.authorAvatar).setOnClickListener(v -> showAuthorInfoDialog());

        // 更多选项按钮点击事件
        findViewById(R.id.btnMore).setOnClickListener(v -> showMoreOptionsDialog());
    }

    private void setupWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    private void showAuthorInfoDialog() {
        new MaterialAlertDialogBuilder(this)
                .setTitle("作者信息")
                .setMessage("杨老师\nAndroid开发讲师\n专注于Android应用开发教学")
                .setPositiveButton("确定", null)
                .show();
    }

    private void showMoreOptionsDialog() {
        String[] options = {"举报", "复制链接", "在浏览器中打开", "设置"};
        new MaterialAlertDialogBuilder(this)
                .setTitle("更多选项")
                .setItems(options, (dialog, which) -> {
                    // 处理选项点击
                    showToast("点击了：" + options[which]);
                })
                .show();
    }

    private void updateLikeButton() {
        btnLike.setText(isLiked ? "已点赞" : "点赞");
        btnLike.setIconResource(isLiked ? R.drawable.ic_heart_filled : R.drawable.ic_heart_outline);
    }

    private void updateFavoriteButton() {
        btnFavorite.setText(isFavorited ? "已收藏" : "收藏");
        btnFavorite.setIconResource(isFavorited ? R.drawable.ic_star_filled : R.drawable.ic_star_outline);
    }

    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}